package conf

import (
	"chainmaker.org/chainmaker/smarteditor/db"
	"github.com/jinzhu/gorm"
)

type UserContractHidden struct {
	UserId   string `gorm:"column:user_id;unique_index:uid_c;index:uid_h"`
	Contract string `gorm:"column:contract;unique_index:uid_c"`
	Hidden   bool   `gorm:"column:hidden;index:uid_h"`
}

func (*UserContractHidden) TableName() string {
	return "user_contract_hidden"
}

// CreateUserContractHidden 创建一下记录
// 新建合约的时候创建一下；rename的时候创建一下；调用loginCallback接口的时候初始化一下
func CreateUserContractHidden(data *UserContractHidden) error {
	return db.GlobalDB.Model(&UserContractHidden{}).Create(data).Error
}

func UpdateUserContractHidden(userId, contract string, hidden int8) error {
	// log.Debugf("UpdateUserContractHidden uid [%s], contract [%s] , hidden %+v",
	// 	userId, contract, hidden)
	return db.GlobalDB.Exec("update user_contract_hidden set hidden = ? where user_id = ? and contract = ?",
		hidden, userId, contract).Error
}

// DeleteUserContractHidden
// 删除合约的时候调用一下；合约改名的时候先delete,再create
func DeleteUserContractHidden(userId, contract string) error {
	return db.GlobalDB.Model(&UserContractHidden{}).Where("user_id = ?", userId).Where("contract = ?", contract).Delete(&UserContractHidden{}).Error
}

// GetUserContractHiddenList 根据用户id,分页查询合约隐藏信息
func GetUserContractHiddenList(userId string, page, pageSize int64) ([]UserContractHidden, int, error) {
	var total int
	var retList []UserContractHidden
	totalErr := db.GlobalDB.Model(&UserContractHidden{}).Where("user_id = ?", userId).Count(&total).Error
	if totalErr != nil {
		log.Errorf("GetUserContractHiddenList count userId %s failed %s", userId, totalErr.Error())
		return retList, total, totalErr
	}
	queryDB := db.GlobalDB.Model(&UserContractHidden{}).Where("user_id = ?", userId)
	var queryErr error
	if pageSize == -1 {
		queryErr = queryDB.Scan(&retList).Error
	} else {
		queryErr = queryDB.Limit(pageSize).Offset((page - 1) * pageSize).Scan(&retList).Error
	}

	if queryErr != nil {
		log.Errorf("GetUserContractHiddenList userId %s, failed %s", userId, queryErr.Error())
		return retList, total, queryErr
	}
	return retList, total, nil
}

// GetUserHiddenContract 查询用户
func GetUserHiddenContract(userId string) ([]UserContractHidden, error) {
	var retList []UserContractHidden
	queryErr := db.GlobalDB.Model(&UserContractHidden{}).Where("user_id = ? and hidden = ?", userId, true).Scan(&retList).Error
	return retList, queryErr
}

func RenameContract(oldName, newName string, uid string) error {
	return db.GlobalDB.Transaction(func(tx *gorm.DB) error {
		delErr := db.GlobalDB.Model(&UserContractHidden{}).
			Where("user_id = ?", uid).Where("contract = ?", oldName).
			Delete(&UserContractHidden{}).Error
		if delErr != nil {
			log.Errorf("RenameContract delete uid %s, old %s, failed %s",
				uid, oldName, delErr.Error())
			return delErr
		}
		createErr := db.GlobalDB.Model(&UserContractHidden{}).Create(&UserContractHidden{
			UserId:   uid,
			Contract: newName,
		}).Error
		if createErr != nil {
			log.Errorf("RenameContract rename uid %s, new %s, failed %s",
				uid, newName, createErr.Error())
			return createErr
		}
		return nil
	})

}
